package net.huashitong.synthetical.showView.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.giszd.service.EarlyWarningNodeService;
import net.huashitong.synthetical.showView.filter.ProjectFilter;
import net.huashitong.synthetical.showView.service.SyntheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = "/earlywarning")
public class EarlyWarningCtrl extends JsonBaseController {
    @Override
    protected String getView(String s) {
        return null;
    }

    @Autowired
    private SyntheService syntheService;

    @Autowired
    private EarlyWarningNodeService earlyWarningService;


    /**
     * 获取预警等级数量
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getnumbers",method = RequestMethod.POST)
    public Message getWarningNumbers(@RequestBody ProjectFilter filter){

        String projectId = filter.getProjectId();
        if (StringUtils.isBlank(filter.getProjectId())) {
            //默认中海创Id
            projectId = "20180312100000000352";
        }
//        调用本系统的数据
//        Map<String, Integer> result = earlyWarningService.queryByWarningNumber(projectId);

        JSONObject result = syntheService.warningnumbers(projectId);
        return coverMessage("200", "成功", result);
    }


    /**
     * 获取督查节点详情
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getwarningdetail",method = RequestMethod.POST)
    public Message getWarningDetail(@RequestBody ProjectFilter filter){
        //等级解释：
        String level = filter.getLevel();
        String projectId = filter.getProjectId();
        if (StringUtils.isBlank(filter.getProjectId())) {
            //默认中海创Id
            projectId = "20180312100000000352";
        }
        if (StringUtils.isBlank(filter.getLevel())) {
            //1等级轻度  2中度 3高度
            level = "1";
        }

//        调用本系统的数据
//        List<EarlyWarningNode> earlyWarningNodeList = earlyWarningService.queryWarningDetail(projectId, level);
//        List<Map<String, Object>> result = new ArrayList<>();
//        for(EarlyWarningNode e : earlyWarningNodeList){
//            Map<String, Object> map = new HashMap<>();
//            map.put("nodeId", e.getNodeId());
//            map.put("problem", StringUtils.isBlank(e.getOpinions())?"待处理":e.getOpinions());
//            map.put("nodeTime", e.getUpdDate());
//            result.add(map);
//        }

        JSONArray result = syntheService.warningdetail(projectId,level);
        return coverMessage("200", "成功", result);
    }


    /**
     * 获取指定项目所有预警列表
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getwarningshort",method = RequestMethod.POST)
    public Message getWarningShort(@RequestBody ProjectFilter filter){
        String projectId = filter.getProjectId();
        if (StringUtils.isBlank(filter.getProjectId())) {
            //默认中海创Id
            projectId = "20180312100000000352";
        }

//        调用本系统的数据
//        List<Map> result = new ArrayList<>();
//        List<EarlyWarningNode> earlyWarningNodes = earlyWarningService.queryByWarningNumberShort(projectId);
//        for (EarlyWarningNode earlyWarningNode : earlyWarningNodes) {
//            Map<String, Object> map = new HashMap<>();
//            map.put("nodeId", earlyWarningNode.getNodeId());
//            map.put("problem", StringUtils.isBlank(earlyWarningNode.getOpinions())?"待处理":earlyWarningNode.getOpinions());
//            map.put("nodeTime", earlyWarningNode.getUpdDate());
//            result.add(map);
//        }

        JSONArray result = syntheService.warningshort(projectId);
        return coverMessage("200", "成功", result);
    }


    /**
     * 统计各部门预警次数
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getearlywarningcountdept",method = RequestMethod.GET)
    public Message getEarlyWarningCountDept(){
        List<Map<String,Object>> resList = earlyWarningService.queryCalculationDept();
        Map<String,Object> result = new HashMap<>();
        List<String> deptNames = new ArrayList<>();
        List<BigInteger> counts = new ArrayList<>();
        for(Map<String,Object> m : resList){
            deptNames.add((String) m.get("resDept"));
            counts.add((BigInteger) m.get("count"));
        }
        result.put("deptNames",deptNames);
        result.put("counts",counts);
        return coverMessage("200", "成功", result);
    }


    /**
     * 统计各大节点预警次数
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getearlywarningcountnode", method = RequestMethod.GET)
    public Message getEarlyWarningCountNode(){
        Map<String,Object> resMap = earlyWarningService.queryCalculationNodeId();
        return coverMessage("200", "成功", resMap);
    }

}
